package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.HistoryDateConfigMapper;
import com.lnking.lnkingplace.model.dto.isolation.afterPositivePersonnel.AfterPositivePersonnelExcel;
import com.lnking.lnkingplace.model.dto.isolation.afterPositivePersonnel.AfterPositivePersonnelListDto;
import com.lnking.lnkingplace.model.entity.HistoryDateConfig;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.isolation.afterPositivePersonnel.AfterPositivePersonnelAddRequest;
import com.lnking.lnkingplace.model.request.isolation.afterPositivePersonnel.AfterPositivePersonnelSearchRequest;
import com.lnking.lnkingplace.model.request.isolation.afterPositivePersonnel.AfterPositivePersonnelUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationAfterPositivePersonnelService;
import com.lnking.starter.export.utils.ExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-21
 */
@RestController
@RequestMapping("/isolation-after-positive-personnel")
@Api(tags = "隔离场所复阳人员")
public class IsolationAfterPositivePersonnelController {

    @Resource
    IIsolationAfterPositivePersonnelService iIsolationAfterPositivePersonnelService;
    @Resource
    HistoryDateConfigMapper historyDateConfigMapper;
    @Value("${epidemic.startDate}")
    String epidemicStartDate;
    @Resource
    UserUtil userUtil;

    @ApiOperation(value = "导出")
    @GetMapping("export")
    public void export(AfterPositivePersonnelSearchRequest params, HttpServletResponse response) throws Exception {
        List<AfterPositivePersonnelListDto> data =
                (List<AfterPositivePersonnelListDto>)iIsolationAfterPositivePersonnelService.pageList(params);
        ExportUtils.export(data, response, AfterPositivePersonnelExcel.class);
    }

    @ApiOperation("导入")
    @PostMapping("import")
    public ReturnResult importData(@RequestParam("file") MultipartFile file) throws Exception{
        iIsolationAfterPositivePersonnelService.importExcel(file);
        return ReturnResult.success();
    }

    @GetMapping("list")
    @ApiOperation("查询列表")
    public ReturnResult queryList(AfterPositivePersonnelSearchRequest params){
        String type = userUtil.getUser().getType();
        //隔离场所
        if(type.equals(UserType.PLACE)){
            params.setPlaceName(userUtil.getUser().getName());
        }

        if("是".equals(params.getThisEpidemic())){
            HistoryDateConfig historyDateConfig = historyDateConfigMapper.selectOne(null);
            params.setEpidemicStartDate(LocalDateTimeUtil.localDateToString(historyDateConfig.getEpidemicDate(),"yyyy-MM-dd"));
        }
        return ReturnResult.success(iIsolationAfterPositivePersonnelService.pageList(params));
    }

    @ApiOperation("新增")
    @PostMapping("add")
    public ReturnResult add(@Valid @RequestBody AfterPositivePersonnelAddRequest params) {
        iIsolationAfterPositivePersonnelService.add(params);
        return ReturnResult.success();
    }

    @ApiOperation("修改")
    @PostMapping("update")
    public ReturnResult update(@Valid @RequestBody AfterPositivePersonnelUpdateRequest params) {
        iIsolationAfterPositivePersonnelService.update(params);
        return ReturnResult.success();
    }

    @ApiOperation("查询详情")
    @GetMapping("get-details")
    public ReturnResult getDetails(String id) {
        return ReturnResult.success(iIsolationAfterPositivePersonnelService.getById(id));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        iIsolationAfterPositivePersonnelService.removeById(id);
        return ReturnResult.success();
    }
}
